import EventCenter from "../../lib/EventCenter";
import MyDialogManager from "../../lib/MyDialogManager";
import EventEunm from "../data/EventEunm";
import PlayerData from "../data/PlayerData";
import RankManager from "../RankManager";
import {ui} from "./../../ui/layaMaxUI";
export default class RankDialog extends ui.RankDialogUI{
    private mdata;
    constructor(){
        super();
        this.list.array=[];
        this.list.renderHandler=new Laya.Handler(this,this.redderr);
    }

    private redderr(_sp:Laya.Sprite,_index:number){
        let tdata = this.list.array[_index];
        _sp.getChildByName("rankTxt")["text"]=(_index+1);
        _sp.getChildByName("nameTxt")["text"]=tdata.name;
        _sp.getChildByName("scoreTxt")["text"]=tdata.score;
    }
    onEnable(){
        this.gaiBtn.on(Laya.Event.CLICK,this,this.gaiClick);
        EventCenter.getIns().on(EventEunm.changeName,this,this.changeName)
        this.closeBtn.once(Laya.Event.CLICK,this,this.close);
        RankManager.getIns().getRankData((_data)=>{
            this.mdata=_data;
            this.tip.visible=false;
            this.list.array=_data;
            this.fresh();
        });
        this.fresh();
    }

    private fresh(){
        this.nameTxt.text=PlayerData.getIns().name?PlayerData.getIns().name:"佚名";
        this.rankTxt.text="排行："+this.getMyRank();
        this.gaiNumTxt.text="x"+PlayerData.getIns().getItemNum("1000001");

    }

    private changeName(){
        this.fresh();
        this.list.array=RankManager.getIns().getListData();
    }

    private gaiClick(){
        MyDialogManager.getIns().sceneOpen("ChangeName.scene");
    }

    private getMyRank(){
        let  tstr= "未上榜";
        if(this.mdata){
            for(let i=0;i<this.mdata.length;i++){
                if(this.mdata[i].id==PlayerData.getIns().id)return i+1;
            }

        }
        return tstr;
    }

    onDisable(){
        EventCenter.getIns().off(EventEunm.changeName,this,this.fresh)
        this.gaiBtn.off(Laya.Event.CLICK,this,this.gaiClick);
    }
}